{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6b3a6ae7-59e0-4498-814e-a93e1d634cdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "%use fuel(2.3.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ab39a624-cec1-4102-b23d-640c312b72dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "val envs = java.io.File(\"../../.env\")\n",
    "    .readLines()\n",
    "    .map {\n",
    "        it.split(\"=\")[0] to it.split(\"=\")[1].trim('\"')\n",
    "    }.toMap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "575b8a31-6e1c-4edc-98f8-72c52992797b",
   "metadata": {},
   "outputs": [],
   "source": [
    "val session = envs.get(\"AOC_SESSION\")\n",
    "val year = 2021\n",
    "val day = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dd799fac-9c82-4c5a-b308-60691a01a866",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun getInput(year: Int, day: Int, session: String): String {\n",
    "    val (_, _, result) = \"https://adventofcode.com/$year/day/$day/input\"\n",
    "    .httpGet()\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cac8a111-9146-49ea-8989-c6392869eaaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun submitAnswer(year: Int, day: Int, session: String, level: Int, answer: String): String {\n",
    "    val (_, _, result) = Fuel\n",
    "    .post(\n",
    "        \"https://adventofcode.com/$year/day/$day/answer\", \n",
    "        parameters = listOf(\"level\" to level, \"answer\" to answer))\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a064468c-9c51-4307-ba19-e3ff83c6c4ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "val sample = \"\"\"forward 5\n",
    "down 5\n",
    "forward 8\n",
    "up 3\n",
    "down 8\n",
    "forward 2\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "878489a4-5e1e-4c04-a209-650185a8de67",
   "metadata": {},
   "outputs": [],
   "source": [
    "val input = getInput(year, day, session)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f80e521a-bf11-4b36-85af-14f9111d262e",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partOne(input: String): String {\n",
    "    val commands = input.split('\\n').filter {\n",
    "        it.length > 0\n",
    "    }.map {\n",
    "        it.split(' ')\n",
    "    }\n",
    "        \n",
    "    var pos = 0\n",
    "    var depth = 0\n",
    "    \n",
    "    for (command in commands) {\n",
    "        val num = command[1].toInt()\n",
    "        \n",
    "        when (command[0]) {\n",
    "            \"forward\" -> pos += num\n",
    "            \n",
    "            \"up\" -> depth -= num\n",
    "\n",
    "            else -> {\n",
    "                depth += num\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "        \n",
    "    return (pos * depth).toString()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "eaa273a8-a166-44e0-9023-2449fecbbb94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "150"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partOne(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2ad99e07-5a96-40c4-873c-e72dd7c5f499",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1488669"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partOneAns = partOne(input)\n",
    "partOneAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c374059-671d-43e6-9566-83c6d3b18de2",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 1, partOneAns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "528387cf-abb5-4da2-9fd3-9bbf865444a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partTwo(input: String): String {\n",
    "   val commands = input.split('\\n').filter {\n",
    "        it.length > 0\n",
    "    }.map {\n",
    "        it.split(' ')\n",
    "    }\n",
    "        \n",
    "    var pos = 0\n",
    "    var depth = 0\n",
    "    var aim = 0\n",
    "    \n",
    "    for (command in commands) {\n",
    "        val num = command[1].toInt()\n",
    "        \n",
    "        when (command[0]) {\n",
    "            \"forward\" -> {\n",
    "                pos += num\n",
    "                depth += aim * num\n",
    "            }\n",
    "            \n",
    "            \"up\" -> aim -= num\n",
    "\n",
    "            else -> aim += num\n",
    "        }\n",
    "    }\n",
    "        \n",
    "    return (pos * depth).toString()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b12a86b5-8d6f-4a6c-851a-af1d84ae168d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "900"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partTwo(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "62a5e94e-550f-42fe-9597-32e6f87a5b1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1176514794"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partTwoAns = partTwo(input)\n",
    "partTwoAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2497e3fd-a979-4709-b5c2-d3ce8804313f",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 2, partTwoAns)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.6.20-dev-5432"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
